<template>
	<div class="system-notice-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="noticeDialogFormRef" :model="state.ruleForm" size="default" label-width="90px"
				:rules="state.rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="通知标题" prop="noticeTitle">
							<el-input v-model="state.ruleForm.noticeTitle" placeholder="请输入通知标题" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="公告类型" prop="noticeType">
							<el-select v-model="state.ruleForm.noticeType" placeholder="请选择">
								<el-option v-for="dict in sys_notice_type" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="公告状态" prop="status">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio v-for="dict in sys_notice_status" :key="dict.value" :label="dict.value">{{
									dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="通知内容" prop="noticeContent">
							<el-input v-model="state.ruleForm.noticeContent" type="textarea" placeholder="请输入内容"
								clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入备注"
								clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" v-auth="'system:notice:edit'">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemNoticeDialog">
import { reactive, ref } from 'vue';
import { useNotice } from '/@/api/system/notice';
import { ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';

const { sys_notice_status, sys_notice_type } = useDict("sys_notice_status", "sys_notice_type");
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const noticeDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		noticeId: 0,
		noticeTitle: '',
		noticeType: '',
		noticeContent: '',
		status: '',
		remark: '',
	},
	rules: {

	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowNoticeType) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改公告';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增公告';
		state.dialog.submitTxt = '新 增';
		getNoticeRuleForm();
	}
	state.dialog.isShowDialog = true;
};

// 提交
const onSubmit = async () => {
	try {
		await noticeDialogFormRef.value.validate(async (valid: any, fields: any) => {
			if (valid) {
				if (state.dialog.type === 'add') {
					await addNotice();
				} else {
					await updateNotice();
				}
			} else {
				ElMessage.error('请检查输入项是否正确');
				return Promise.reject(fields);
			}
		});
	} catch (err) {
		Promise.reject(err);
	}
};
const addNotice = async () => {
	try {
		const res = await useNotice().addNotice({
			noticeTitle: state.ruleForm.noticeTitle,
			noticeType: state.ruleForm.noticeType,
			noticeContent: state.ruleForm.noticeContent,
			status: state.ruleForm.status,
			remark: state.ruleForm.remark,
		});
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};
const updateNotice = async () => {
	try {
		const res = await useNotice().updateNotice(state.ruleForm);
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};


// 初始化公告数据
const getNoticeRuleForm = () => {
	state.ruleForm = {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		noticeId: 0,
		noticeTitle: '',
		noticeType: '',
		noticeContent: '',
		status: '',
		remark: '',
	};
};
// 取消
const onCancel = () => {
	closeDialog();
	getNoticeRuleForm();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};// 暴露变量
defineExpose({
	openDialog,
});
</script>